const state = {
  hehunUserId: "3351725636042375168", // 用户ID
  // 用户信息
  hehunUserInfo: [
    {
      userId: "3351708854439133184",
      userPhone: null,
      userSurname: "张",
      userName: "er",
      userGender: "男",
      shuXing: "龙",
      lunarDay: "一九八八年正月初十巳时",
      solarDay: "1988-02-26 10:10:00",
      birthYear: 1988,
      birthMonth: 2,
      birthDay: 26,
      birthHour: 10,
      birthMinute: 10,
      yearZhu: "戊辰",
      monthZhu: "甲寅",
      dayZhu: "辛亥",
      hourZhu: "癸巳",
      yearGan: "戊",
      monthGan: "甲",
      dayGan: "辛",
      hourGan: "癸",
      yearZhi: "辰",
      monthZhi: "寅",
      dayZhi: "亥",
      hourZhi: "巳",
      sourceForm: "test",
      woodCount: 2,
      fireCount: 1,
      earthCount: 2,
      metalCount: 1,
      waterCount: 2,
      woodStatus: "平",
      fireStatus: "平",
      earthStatus: "平",
      metalStatus: "平",
      waterStatus: "平",
      yearGanShiShen: "正印",
      monthGanShiShen: "正财",
      dayGanShiShen: "日主",
      hourGanShiShen: "食神",
      yearZhiShiShen: "正印、偏财、食神",
      monthZhiShiShen: "正财、正官、正印",
      dayZhiShiShen: "伤官、正财",
      hourZhiShiShen: "正官、劫财、正印",
      hyShiShen: "正财：甲  偏财：乙",
      touGan: "月干透出",
      fqxLocation: "月柱",
      fqxCount: 1,
      yearNaYin: "大林木",
      monthNaYin: "大溪水",
      dayNaYin: "钗钏金",
      hourNaYin: "长流水",
      basicWuxing: "木",
      mgCount: 3,
      mgName: "震宫",
      zhengCount: 3,
      fanCount: 1,
      heHun: true,
      heHunId: "3351708854321692672",
      createTime: "2025-04-27 22:54:23",
      xingZuo: "双鱼",
    },
    {
      userId: "3351708854816620544",
      userPhone: null,
      userSurname: "马",
      userName: "六",
      userGender: "女",
      shuXing: "兔",
      lunarDay: "一九八七年腊月廿三亥时",
      solarDay: "1988-02-10 22:22:00",
      birthYear: 1988,
      birthMonth: 2,
      birthDay: 10,
      birthHour: 22,
      birthMinute: 22,
      yearZhu: "丁卯",
      monthZhu: "甲寅",
      dayZhu: "乙未",
      hourZhu: "丁亥",
      yearGan: "丁",
      monthGan: "甲",
      dayGan: "乙",
      hourGan: "丁",
      yearZhi: "卯",
      monthZhi: "寅",
      dayZhi: "未",
      hourZhi: "亥",
      sourceForm: "test",
      woodCount: 4,
      fireCount: 2,
      earthCount: 1,
      metalCount: 0,
      waterCount: 1,
      woodStatus: "旺",
      fireStatus: "平",
      earthStatus: "平",
      metalStatus: "弱",
      waterStatus: "平",
      yearGanShiShen: "正财",
      monthGanShiShen: "劫财",
      dayGanShiShen: "日主",
      hourGanShiShen: "食神",
      yearZhiShiShen: "正财、比肩、偏印",
      monthZhiShiShen: "劫财、伤官、正财",
      dayZhiShiShen: "偏财、食神、比肩",
      hourZhiShiShen: "正印、劫财",
      hyShiShen: "正官：辛  偏官：庚",
      touGan: "多干透出",
      fqxLocation: "未知",
      fqxCount: 0,
      yearNaYin: "炉中火",
      monthNaYin: "大溪水",
      dayNaYin: "沙中金",
      hourNaYin: "屋上土",
      basicWuxing: "火",
      mgCount: 3,
      mgName: "震宫",
      zhengCount: 0,
      fanCount: 0,
      heHun: true,
      heHunId: "3351708854321692672",
      createTime: "2025-04-27 22:54:23",
      xingZuo: "水瓶",
    },
  ],
  // 合婚用户信息处理后的结构
  otherHehunUserInfo: [],
  // 其他信息
  hehunResultInfo: [
    {
      name: "命宫/五行",
      content: [
        {
          title: "命宫",
          content: "",
        },
        {
          title: "五行",
          content: "",
        },
        {
          title: "生肖属相",
          content: "",
        },
      ],
    },
    {
      name: "星座",
      content: [
        {
          title: "星座",
          content: "",
        },
      ],
    },
    {
      name: "月令/日干",
      content: [
        {
          title: "八字",
          content: "",
        },
        {
          title: "月令",
          content: "",
        },
        {
          title: "日干",
          content: "",
        },
      ],
    },
  ],
};

const mutations = {
  SET_HEHUN_USER_ID(state, id) {
    state.hehunUserId = id;
  },
  SET_HEHUN_USER_INFO(state, data) {
    state.hehunUserInfo = data;
  },
  SET_HEHUN_RESULT_INFO(state, data) {
    state.hehunResultInfo = data;
  },
  SET_OTHER_HEHUN_USER_INFO(state, data) {
    // 如果data为空或不是数组，直接设置为空数组
    if (!data || !Array.isArray(data) || data.length === 0) {
      state.otherHehunUserInfo = [];
      return;
    }

    const maleData =
      data.find((item) => item?.basicInfo?.gender === "男") || data[0];
    const femaleData =
      data.find((item) => item?.basicInfo?.gender === "女") || data[1];
      
    // 如果找不到有效数据，设置为空数组
    if (!maleData || !femaleData) {
      state.otherHehunUserInfo = [];
      return;
    }
      
    // 提取请求的字段到对象并存入数组
    state.otherHehunUserInfo = [
      {
        ...maleData,
        gender: maleData.basicInfo?.userGender || "",
        userName: maleData.basicInfo?.userName || "",
        solarDay: (maleData.basicInfo?.solarDay || "").split(" ")[0],
        shuXing: maleData.basicInfo?.shuXing || "",
        xingZuo: maleData.basicInfo?.xingZuo || "",

        sizhu: `${maleData.baZiInfo?.yearZhu || ""}/${
          maleData.baZiInfo?.monthZhu || ""
        }/${maleData.baZiInfo?.dayZhu || ""}/${maleData.baZiInfo?.hourZhu || ""}`,
        
        mgName: maleData.heHunInfo?.mgName || "",
        yearNaYin: maleData.heHunInfo?.yearNaYin || "",
        basicWuxing: maleData.heHunInfo?.basicWuxing || "",
      },
      {
        ...femaleData,
        gender: femaleData.basicInfo?.userGender || "",
        userName: femaleData.basicInfo?.userName || "",
        solarDay: (femaleData.basicInfo?.solarDay || "").split(" ")[0],
        shuXing: femaleData.basicInfo?.shuXing || "",
        xingZuo: femaleData.basicInfo?.xingZuo || "",

        sizhu: `${femaleData.baZiInfo?.yearZhu || ""}/${
          femaleData.baZiInfo?.monthZhu || ""
        }/${femaleData.baZiInfo?.dayZhu || ""}/${
          femaleData.baZiInfo?.hourZhu || ""
        }`,

        mgName: femaleData.heHunInfo?.mgName || "",
        yearNaYin: femaleData.heHunInfo?.yearNaYin || "",
        basicWuxing: femaleData.heHunInfo?.basicWuxing || "",
      },
    ];
  },
};

const actions = {
  setHehunUserId({ commit }, id) {
    commit("SET_HEHUN_USER_ID", id);
    return Promise.resolve();
  },
  setHehunUserInfo({ commit }, hehunUserInfo) {
    commit("SET_HEHUN_USER_INFO", hehunUserInfo);
    return Promise.resolve();
  },
  setHehunResultInfo({ commit }, hehunResultInfo) {
    commit("SET_HEHUN_RESULT_INFO", hehunResultInfo);
    return Promise.resolve();
  },
  setOtherHehunUserInfo({ commit }, data) {
    commit("SET_OTHER_HEHUN_USER_INFO", data);
    return Promise.resolve();
  },
};

const getters = {
  hehunUserId: (state) => state.hehunUserId,
  hehunUserInfo: (state) => state.hehunUserInfo,
  hehunResultInfo: (state) => state.hehunResultInfo,
  otherHehunUserInfo: (state) => state.otherHehunUserInfo,
};

export default {
  namespaced: true,
  state,
  getters,
  mutations,
  actions,
};
